package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.ScAddGwcBean;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.utils.MyRecyclerView;
import com.lwj.liwanjia.utils.RxBus;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/12.
 */

public class SelectAdapter1 extends SuperBaseAdapter<ScAddGwcBean.DataBean.LableBean> {
    private Context context;
    private Map<String, Object> map;
    private int size = 0;//标签个数
    private RxBus rxBus;

    public void setSize(int size) {
        this.size = size;
    }

    public SelectAdapter1(Context context, List<ScAddGwcBean.DataBean.LableBean> data) {
        super(context, data);
        this.context = context;
        rxBus = RxBus.getInstance();
        map = new HashMap<>();
    }

    @Override
    protected void convert(BaseViewHolder holder, final ScAddGwcBean.DataBean.LableBean item, int position) {
        holder.setText(R.id.tv_biaoti, item.getTitle());
        final String[] lable = item.getType().split(";");
        MyRecyclerView rlvFlex = holder.getView(R.id.rlv_flex);
        FlexboxLayoutManager manager = new FlexboxLayoutManager();
        //设置主轴排列方式
        manager.setFlexDirection(FlexDirection.ROW);
        //设置是否换行
        manager.setFlexWrap(FlexWrap.WRAP);
        manager.setAlignItems(AlignItems.STRETCH);
        manager.setJustifyContent(JustifyContent.FLEX_START);
        rlvFlex.setLayoutManager(manager);
        final SelectAdapter adapter = new SelectAdapter(lable, context);
        rlvFlex.setAdapter(adapter);
        adapter.setListener(new com.lwj.liwanjia.config.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                adapter.setCheckPosition(position);
                adapter.notifyDataSetChanged();
                map.put(item.getTitle(), lable[position]);
                if (map.size() == size) {
                    rxBus.post(map);
                }
            }
        });
    }

    @Override
    protected int getItemViewLayoutId(int position, ScAddGwcBean.DataBean.LableBean item) {
        return R.layout.select_item;
    }

    public Map<String, Object> getMaps() {
        return map;
    }
}
